package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IBaseQuarterSetDao;
import com.magicode.cms.entiy.base.BaseQuarterSet;
import com.magicode.cms.service.IBaseQuarterSetService;

@Service("servicebaseQuarterSet")
public class BaseQuarterSetServiceImpl extends BaseServiceImpl<BaseQuarterSet, IBaseQuarterSetDao> implements IBaseQuarterSetService {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "type");
		map.put(json_title, "类型");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "program");
		map.put(json_title, "关联节目");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "quarterCount");
		map.put(json_title, "季度");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "setCount");
		map.put(json_title, "集");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "companyName");
		map.put(json_title, "冠名公司");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "audienceRating");
		map.put(json_title, "收视率");
		map.put("width", 120);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql =" from " +BaseQuarterSet.class.getName()+" "+entity_alias_name+" ";
//		hql += " left join "+entity_alias_name+".type ";
		hql += " left join "+entity_alias_name+".program ";
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1 ";
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
//			gridsel.put("type", ""+entity_alias_name+".type.title as type ");
			gridsel.put("program", ""+entity_alias_name+".program.titleCn as program ");
			gridsel.put("quarterCount", ""+entity_alias_name+".quarterCount as quarterCount ");
			gridsel.put("setCount", ""+entity_alias_name+".setCount as setCount ");
			gridsel.put("companyName", ""+entity_alias_name+".companyName as companyName ");
			gridsel.put("audienceRating", ""+entity_alias_name+".audienceRating as audienceRating ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public IBaseQuarterSetDao getDao() throws Exception {
		return baseQuarterSetDao;
	}
	
	@Autowired
	@Qualifier("daobaseQuarterSet")
	private IBaseQuarterSetDao baseQuarterSetDao;

	public IBaseQuarterSetDao getBaseQuarterSetDao() {
		return baseQuarterSetDao;
	}

	public void setBaseQuarterSetDao(IBaseQuarterSetDao baseQuarterSetDao) {
		this.baseQuarterSetDao = baseQuarterSetDao;
	}
	
}
